package com.silence.basic_learn;

import java.util.Objects;

public class Student implements Comparable<Student>{
    String name;
    int age;
    double chinese;
    double math;
    public static int StudentNum;
    private double score;

    static { // 静态代码块，初始化
        StudentNum = 0;
    }

    @Override
    public int compareTo(Student o) {
        //约定1：左对象 > 右对象 返回正整数
        //约定2：左对象 < 右对象 返回负整数
        //约定3：左对象 = 右对象 返回0
//        if(this.age > o.age){
//            return 1;
//        }else if (this.age < o.age){
//            return -1;
//        }else{
//            return 0;
//        }
        return this.age-o.age; //升序
//        return o.age-this.age; //降序
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", chinese=" + chinese +
                ", math=" + math +
                ", score=" + score +
                '}';
    }

    //无参构造器
    public Student() {
        Student.StudentNum++;
        System.out.println("无参构造器");
    }

    //有参构造器
    public Student(String name, double chinese, double math) {
        System.out.println("有参构造器");
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    public void setScore(double score) {
        if (score >= 0 && score <= 100) {
            this.score = score;
        } else {
            System.out.println("分数非法");
        }
    }

    public double getScore() {
        return this.score;
    }

    // 暂时不加static
    public void printTotalScore() {
        System.out.println(name + "总成绩:" + (chinese + math));
    }

    public void printAverageScore() {
        System.out.println(name + "平均成绩:" + (chinese + math) / 2);
    }

    public void compare() {
        if (this.math > 90) {
            System.out.println("Your math is good");
        } else {
            System.out.println("You math is not good");
        }
    }

    public static int getStudentNum() {
        return StudentNum;
    }

    //当对象存储于HashSet中，或存于HashMap的键中时
    //若要使两个内容相同的对象能被hashSet去重
    //则需要重写hashCode和equals
    //右键generate 然后选就行，系统帮忙修改
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Double.compare(student.chinese, chinese) == 0 && Double.compare(student.math, math) == 0 && Double.compare(student.score, score) == 0 && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, chinese, math, score);
    }
}
